package com.ruoyi.system.utils;




import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

/**
 * MD5加密类（封装jdk自带的md5加密方法）
 *
 */
public class MD5Util {

    public static String encrypt(String source) {
        return encodeMd5(source.getBytes());
    }

    private static String encodeMd5(byte[] source) {
        try {
            return encodeHex(MessageDigest.getInstance("MD5").digest(source));
        } catch (NoSuchAlgorithmException e) {
            throw new IllegalStateException(e.getMessage(), e);
        }
    }

    public  static String encrypt(String source,String salt){
        String str="";
        str=encrypt(source+salt);
        return  str;
    }
    private static String encodeHex(byte[] bytes) {
        StringBuffer buffer = new StringBuffer(bytes.length * 2);
        for (int i = 0; i < bytes.length; i++) {
            if (((int) bytes[i] & 0xff) < 0x10)

                buffer.append("0");
            buffer.append(Long.toString((int) bytes[i] & 0xff, 16));
        }
        return buffer.toString();
    }

    public static boolean checkPassword(String password, String salt,String md5Str){
        boolean b=false;
        String str=encrypt(password,salt);
        if(str.equals(md5Str))
            b=true;
        return  b;
    }

/*    @Test
    public  void test() {
        String str= RandomUtil.randomStr(6);
        System.out.println(str);
        System.out.println(encrypt("111111",str));
        System.out.println(encrypt("21232f297a57a5a743894a0e4a801fc3"));
        System.out.println(checkPassword("111111","B6MVej","74351684bd556d6e4d831c7514870490"));
    }*/

}
